<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.jason.imagesnode.MyStringUtil"%>
<%@page import="org.json.JSONObject"%>
<%@page import="java.util.UUID"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page import="sun.misc.BASE64Decoder"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.io.File"%>

<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="java.awt.image.RenderedImage"%>
<%@ page import="java.io.IOException"%>
<%@ page import="javax.imageio.ImageIO"%>
<%@ page import="org.icepdf.core.pobjects.Document"%>
<%@ page import="org.icepdf.core.util.GraphicsRenderingHints"%>

<%
	String sId = UUID.randomUUID().toString();
	String fileName = "";
	
	response.setHeader("Access-Control-Allow-Origin","*");

	JSONObject obj = new JSONObject();
	obj.put("result", "Y");
	boolean flag = true;
	try {
		request.setCharacterEncoding("UTF-8");
		
		String postfixName = (String)request.getParameter("postfixName");// 服务器前缀
		if(MyStringUtil.isBlank(postfixName)){
			postfixName = "";
		}
		
		String content = (String)request.getParameter("content");  
		String ext = (String)request.getParameter("ext"); 
		byte[] buffer = content.getBytes("utf-8");
		
		String basePath = getServletContext().getRealPath("/");
		String filename = UUID.randomUUID().toString().replace("-", "");
		filename = MyStringUtil.getFileName(filename,postfixName);
		String filepath = MyStringUtil.getFilePath(filename);
		File f = new File(basePath + filepath);
		if (!f.exists()) {
			f.mkdirs();
		}
		
		String filenames = filename + "." + ext;// 文件名
		
		FileOutputStream outfile = new FileOutputStream(basePath + filepath+"/" + filename + "." + ext);
		outfile.write(buffer);
		outfile.close();
		
		System.out.println(basePath + filepath+"/" + filename + "." + ext);
		
		// 如果是pdf就进行切片
		if("pdf".equalsIgnoreCase(ext)){
			String pdfPath = basePath + filepath+"/" + filename + "." + ext;
			String path = basePath + filepath+"/";
			
			Document document = new Document();
	        document.setFile(pdfPath);
	        float scale = 1.5f;//缩放比例
	        float rotation = 0f;//旋转角度

	        for (int i = 0; i < document.getNumberOfPages(); i++) {
	            BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);
	            RenderedImage rendImage = image;
	            try {
	                String imgName = filename + "_" + i + ".png";
	                System.out.println(imgName);
	                File file = new File(path + imgName);
	                ImageIO.write(rendImage, "png", file);
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	            image.flush();
	        }
	        document.dispose();
	        
	        filenames = filename + "_" + document.getNumberOfPages() + "." + ext;// 文件名
		}
		
		obj.put("filename",filenames);
	} catch (Exception e) {
		obj.put("errMsg", e);
		obj.put("result", "N");
		e.printStackTrace();
	} finally {
		try {
			
		} catch (Exception e) {
		}
		//out.println("Access-Control-Allow-Origin:*\r\n");
		out.println(obj.toString());
	}
%>